package com.systop.ltbook.book.subject.webapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.cms.utils.PageUtil;
import com.systop.core.dao.support.Page;
import com.systop.core.webapp.struts2.action.ExtJsCrudAction;
import com.systop.ltbook.book.subject.model.Subject;
import com.systop.ltbook.book.subject.service.SubjectManager;

/**
 * <code>Subject</code>对象的struts2 action。
 * 
 * @author DU
 */
@SuppressWarnings({"serial","unchecked"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class SubjectAction extends ExtJsCrudAction<Subject, SubjectManager>{

	/**
	 * 学科查询列表
	 */
  @Override
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer hql = new StringBuffer("from Subject s where 1=1 ");
		List args = new ArrayList();

		//按学科名称查询
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and s.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		
		page = getManager().pageQuery(page, hql.toString(), args.toArray());
		restorePageData(page);
		return INDEX;
	}
  
  /**
	 * 删除学科类别
	 */
	@Override
  public String remove() {
  	Subject subject = getManager().get(getModel().getId());
  	if (subject.getBooks().size() != 0) {
  		addActionError("该学科涉及图书信息，不能删除！");
			return INDEX;
  	}
  	
  	return super.remove();
  }
}
